/*
 * @Author: yumao 123456
 * @Date: 2023-05-26 15:53:54
 * @LastEditors: yumao 123456
 * @LastEditTime: 2024-02-22 10:46:11
 * @FilePath: \zyDataScreen\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import router from '../router'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api 的 base_url
  timeout: 50000 // request timeout
})

// request拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    //Do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error && error.response) {
      switch (error.response.status) {
        case 302:
          Message.warning({
            message: '连接超时，请重新登录'
          })
         
          break
        case 400:
          error.message = '请求出错'
          break
        case 401:
          Message.warning({
            message: '连接超时，请重新登录'
          })
         
          break
        case 403:
          error.message = '拒绝访问'
          break
        case 404:
          error.message = '请求错误,未找到该资源';
         
          break
        case 500:
          error.message = '服务器端出错'
          break
      }
    } else {
        error.message = '连接服务器失败'
    }
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
